package org.mikhaylov.cp.service.command.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.mikhaylov.cp.service.command.ServiceCommand;
import org.mikhaylov.cp.service.db.entities.City;
import org.mikhaylov.cp.service.db.entities.Inventory;
import org.mikhaylov.cp.service.db.entities.InventoryType;
import org.mikhaylov.cp.service.db.entities.Region;
import org.mikhaylov.cp.service.hibernate.HibernateManager;
import org.mikhaylov.cp.service.response.ResponseType;

public class GetInventoriesServiceCommand extends ServiceCommand {

	private static final long serialVersionUID = 8387718707438759188L;
	private InventoryType inventoryType = null;
	private Double minPrice = null;
	private Double maxPrice = null;
	private Region region = null;
	private City city = null;

	@Override
	protected boolean checkParams(HttpServletRequest request) {
		if (request.getAttribute("inventoryType") != null
				&& !(request.getAttribute("inventoryType") instanceof Long)
				&& !(request.getAttribute("inventoryType") instanceof InventoryType))
			return false;

		if (request.getAttribute("minPrice") != null
				&& !(request.getAttribute("minPrice") instanceof Double))
			return false;

		if (request.getAttribute("maxPrice") != null
				&& !(request.getAttribute("maxPrice") instanceof Double))
			return false;
		if (request.getAttribute("region") != null) {
			if (request.getAttribute("city") != null
					&& !(request.getAttribute("city") instanceof Long)
					&& !(request.getAttribute("city") instanceof City)) {
				return false;
			} else {
				if (!(request.getAttribute("region") instanceof Long)
						&& !(request.getAttribute("region") instanceof Region)) {
					return false;
				}
			}
		}

		return true;
	}

	@Override
	protected boolean extractParams(HttpServletRequest request) {
		if (request.getAttribute("inventoryType") != null) {

			inventoryType = (request.getAttribute("inventoryType") instanceof InventoryType) ? (InventoryType) request
					.getAttribute("inventoryType") : HibernateManager
					.getInstance().getInventoryType(
							(Long) request.getAttribute("inventoryType"));

		}

		if (request.getAttribute("minPrice") != null) {
			minPrice = (Double) request.getAttribute("minPrice");
		}

		if (request.getAttribute("maxPrice") != null) {
			maxPrice = (Double) request.getAttribute("maxPrice");
		}

		if (request.getAttribute("region") != null) {
			region = (request.getAttribute("region") instanceof Region) ? (Region) request
					.getAttribute("region") : HibernateManager.getInstance()
					.getRegion((Long) request.getAttribute("region"));
		}

		if (request.getAttribute("city") != null) {
			city = (request.getAttribute("city") instanceof City) ? (City) request
					.getAttribute("city") : HibernateManager.getInstance()
					.getCity((Long) request.getAttribute("city"));
		}

		return true;
	}

	@Override
	protected void process(HttpServletRequest request) {

		List<Inventory> invs = HibernateManager.getInstance().getInventories(
				inventoryType, minPrice, maxPrice, region, city);

		response.setType(ResponseType.SUCCESS);
		response.setMeta("Inventories extracted successfully");
		request.setAttribute("inventories", invs);
	}

}
